<template>
	<view class="container">
		<!-- 空白页 -->
		<view v-if="!hasLogin || empty===true" class="empty">
			<image src="https://xn-panda-test.oss-cn-hangzhou.aliyuncs.com/xiaochnegxu/cltynull.png" mode="aspectFit">
			</image>
			<view v-if="hasLogin" class="empty-tips">
				空空如也
				<navigator class="navigator" v-if="hasLogin" url="../commodity/commodity" open-type="switchTab">随便逛逛>
				</navigator>
			</view>
			<view v-else class="empty-tips">
				空空如也
				<view class="navigator" @click="navToLogin">去登陆></view>
			</view>
		</view>
		<view v-else>
			<!-- 列表 -->
			<view class="" v-for="(a,b) in cartList" :key="cartList">
				<view class="cart-list">
					<view class="yticon checkbox catyopn" :class="{checked: a.checked}" @click="check('hxcaty',b)">
						<van-icon v-if="cartList[b].checked == false" name="passed" color="#c0c4cc" size="20px" />
						<van-icon name="checked" v-else size="20px" />
						<van-icon name="shop-o" />&nbsp;
						<span>{{a.holdUserName}}</span>
					</view>
					<view v-for="(item,index) in cartList[b].commodities" :key="cartList[b].commodities">
						<van-swipe-cell :async-close="true" @close="onClose" :right-width="65"
							v-for="(i,g) in item.commoditySpecs" :key="item.commoditySpecs">
							<block>
								<view class="cart-item" :class="{'b-b': index!==cartList.length-1}">
									<image :src="item.mainImages[0]" class="lochent" :class="[item.loaded]"
										@click="lodesemd(b,index)"></image>
									<view class="yticon icon-xuanzhong2 checkbox" :class="{checked: i.checked}"
										@click="check('item', b,index,g)"></view>

									<view class="item-right">
										<text class="clamp title">{{item.title}}</text>
										<text class="attr">规格:{{item.commoditySpecs[g].specName}}</text>
										<text class="price">¥{{item.commoditySpecs[g].price}}</text>
										<view>
											<van-stepper class="caty-opster" :value="item.commoditySpecs[g].number"
												v-module="item.commoditySpecs[g].number" integer button-size="24px"
												@plus="actehon(b,index,g)" @minus="formula(b,index,g)" />
										</view>
									</view>
								</view>


							</block>

							<view :slot="right" class="actype" @click="deleteCartItem(b,index,g)">
								<van-icon class="unlik" name="delete-o" size="24" />
								<view class="unlike">
									删&nbsp;除
								</view>
							</view>
						</van-swipe-cell>
					</view>
				</view>
			</view>
			<!-- 底部菜单栏 -->
			<view class="action-section">
				<view class="checkbox">
					<image :src="allChecked?'/static/selected.png':'/static/select.png'" mode="aspectFit"
						@click="check('all')"></image>
					<view class="clear-btn" :class="{show: allChecked}" @click="clearCart">
						删除
					</view>
				</view>
				<view class="total-box">
					<text class="price">¥{{total}}</text>
				</view>
				<button type="primary" class="no-border confirm-btn" @click="createOrder">去结算</button>
			</view>
		</view>
	</view>
</template>

<script>
	import Api from '@/http/api.js'
	export default {

		data() {
			return {
				total: 0, //总价格
				allChecked: false, //全选状态  true|false
				empty: false, //空白页现实  true|false
				cartList: [], //这个是啥 数据
				hasLogin: '',
				q: {
					index: 1,
					size: 100
				},
				add: {
					commodityId: '',
					specId: '',
					holdUserId: '',
					number: Number
				},
				y: {
					shopCartId: '',
					shopCartDetailIds: [],
					all: false
				}
			};
		},
		onLoad() {
			this.loadData();
			this.loachonlist()
		},
		watch: {
			//显示空白页
			cartList(e) {
				let empty = e.length === 0 ? true : false;
				if (this.empty !== empty) {
					this.empty = empty;
				}
			}
		},
		methods: {
			//请求数据
			async loadData() {
				let that = this
				wx.getStorage({ //获取本地缓存
					key: "token",
					success: function(res) {
						that.hasLogin = res.data
					},
				})
				let res = await Api.trolley(this.q)


				this.cartList = res.data;

				this.calcTotal(); //计算总价
			},
			loachonlist() {
				let catyche = this.cartList
				let cartList = catyche.map(item => {
					item.checked = false;
					return item;
				});
				for (let i = 0; i < cartList.length; i++) {
					let cart = cartList[i].commodities.map(item => {
						item.checked = false;
						return item;
					})
					for (let j = 0; j < cartList[i].commodities.length; j++) {
						// for(let h = 0;h<cartList[i].commodities[j].commoditySpecs.length;h++){
						let caty = cartList[i].commodities[j].commoditySpecs.map(item => {
							item.checked = false;
							return item;
						})
						// }
					}
				}
			},
			// //监听image加载完成
			// onImageLoad(key, index) {
			// 	this.$set(this[key][index], 'loaded', 'loaded');
			// },
			// //监听image加载失败
			// onImageError(key, index) {
			// 	this[key][index].image = '/static/errorImage.jpg';
			// },
			navToLogin() {
				uni.navigateTo({
					url: '/pages/public/login'
				})
			},
			//选中状态处理
			check(type, b, index, g) {
				if (type === 'item') {
					this.cartList[b].commodities[index].commoditySpecs[g].checked = !this.cartList[b].commodities[index]
						.commoditySpecs[g].checked;
				} else if (type === 'hxcaty') {
					const list = this.cartList;
					this.cartList[b].checked = !this.cartList[b].checked
					for (let index = 0; index < list[b].commodities.length; index++) {
						let item = list[b]
						item.checked = this.cartList[b].checked
						let goodsList = item.commodities[index].commoditySpecs;
						for (let i = 0; i < goodsList.length; i++) {
							goodsList[i].checked = this.cartList[b].checked
						}
					}
					this.allChecked = this.cartList[b].checked
				} else if (type === 'all') {
					this.allChecked = !this.allChecked
					let cartList = this.cartList;
					for (let i = 0; i < cartList.length; i++) {
						let item = cartList[i];
						item.checked = this.allChecked;
						let goodsList = item.commodities;
						for (let i = 0; i < goodsList.length; i++) {
							let goodsItem = goodsList[i];
							goodsItem.checked = this.allChecked;
							let jsoinfo = goodsItem.commoditySpecs;
							for (let t = 0; t < jsoinfo.length; t++) {
								let goodsinfo = jsoinfo[t];
								goodsinfo.checked = this.allChecked;
							}
						}
					}
					this.allChecked = this.allChecked
				}
				this.calcTotal(type)
			},
			// 数量增加
			async actehon(b, index, g) {
				this.add.number = ++this.cartList[b].commodities[index].commoditySpecs[g].number
				console.log(this.add.number)
				this.add.commodityId = this.cartList[b].commodities[index].commodityId
				this.add.specId = this.cartList[b].commodities[index].commoditySpecs[g].specId
				this.add.holdUserId = this.cartList[b].holdUserId
				const res = await Api.addition(this.add)
				this.calcTotal()
			},
			// 数量减少
			async formula(b, index, g) {
				this.add.number = --this.cartList[b].commodities[index].commoditySpecs[g].number
				this.add.commodityId = this.cartList[b].commodities[index].commodityId
				this.add.specId = this.cartList[b].commodities[index].commoditySpecs[g].specId
				this.add.holdUserId = this.cartList[b].holdUserId
				const res = await Api.subtraction(this.add)
				this.calcTotal()
			},
			// 跳转商品详情
			lodesemd(b, index) {
				const aa = this.cartList[b].commodities[index].commodityId
				uni.navigateTo({
					url: `/pages/cart/loginest?id=` + aa,
				})
			},

			//删除
			deleteCartItem(b, index, g) {
				uni.showModal({
					content: '删除商品？',
					success: async (e) => {
						if (e.confirm) {
							this.y.shopCartId = this.cartList[b].shopCartId
							this.y.shopCartDetailIds.push(this.cartList[b].commodities[index].commoditySpecs[g]
								.shopCartDetailId)
							const rety = await Api.hdelete(this.y)
							this.loadData();
							this.y.shopCartDetailIds = []
							this.onClose()
						}
					},
				})
			},
			// 关闭单元格 
			onClose(event) {
				event.detail.instance.close();
			},
			//清空
			clearCart() {
				uni.showModal({
					content: '删除商品？',
					success: async (e) => {
						console.log(e)
						if (e.confirm) {
							for (let i = 0; i < this.cartList.length; i++) {
								let item = this.cartList[i].commodities
								this.y.shopCartId = this.cartList[i].shopCartId
								for (let j = 0; j < item.length; j++) {
									let arr = item[j].commoditySpecs
									for (let index = 0; index < arr.length; index++) {
										let infolist = arr[index]
										if (infolist.checked == true) {
											this.y.shopCartDetailIds.push(infolist.shopCartDetailId)
										}
									}
								}
							}
							const rety = await Api.hdelete(this.y)
							this.loadData();
							this.y.shopCartDetailIds = []
						} else {
							console.log('123', this.y)
						}
					},
				})

			},
			//计算总价
			calcTotal() {
				let list = this.cartList;
				let total = 0;
				for (let i = 0; i < list.length; i++) {
					let item = list[i].commodities
					for (let j = 0; j < item.length; j++) {
						let goodsli = item[j].commoditySpecs
						for (let index = 0; index < goodsli.length; index++) {
							let info = goodsli[index]
							if (info.checked == true) {
								total += info.price * info.number;
							}
						}
					}
				}

				this.total = total
			},
			//创建订单
			createOrder() {
				let list = this.cartList;
				let goodsData = [];
				let goods = []
				for (let i = 0; i < list.length; i++) {
					goodsData.push({"name":list[i].holdUserName,"commodities":[],"shopCartId":list[i].shopCartId})
					let item = list[i].commodities
					for (let j = 0; j < item.length; j++) {
						let goodsli = item[j].commoditySpecs
						for (let index = 0; index < goodsli.length; index++) {
							let info = goodsli[index]
							if (info.checked == true) {
								goods.push(info)
							   // goodsData.push({"name":list[i].holdUserName,"commodities":item[j].mainImages,"title":item[j].title,"commoditySpecs":info})
							   goodsData[i].commodities.push({"mainImages":item[j].mainImages,"title":item[j].title,"commoditySpecs":info,"commodityId":item[j].commodityId })
							 
							}
						}
					}
				}
				if (goods.length != 0) {
					uni.navigateTo({
					url:`/package/payment/cherorder?data=${JSON.stringify(goodsData)}&type=cart`
					})
				} else {
					this.$api.msg('请勾选要购买的商品');
				}

			},
		}
	}
</script>

<style lang='scss'>
	.container {
		padding-bottom: 134upx;

		/* 空白页 */
		.empty {
			position: fixed;
			overflow: none;
			left: 0;
			top: 0;
			width: 100%;
			height: 100vh;
			padding-bottom: 100upx;
			display: flex;
			justify-content: center;
			flex-direction: column;
			align-items: center;
			background: #fff;
			image {
				width: 290upx;
				height: 240upx;
				margin-bottom: 30upx;
			}
			.empty-tips {
				display: flex;
				font-size: $font-sm+2upx;
				color: $font-color-disabled;
				.navigator {
					color: $uni-color-primary;
					margin-left: 16upx;
				}
				
			}
		}
	}

	/* 购物车列表项 */
	.cart-item {
		display: flex;
		position: relative;
		padding: 30upx 40upx;

		.image-wrapper {
			width: 230upx;
			height: 230upx;
			flex-shrink: 0;
			position: relative;

		}

		image {
			width: 230upx;
			height: 230upx;
			flex-shrink: 0;
			position: relative;
			border-radius: 8upx;
		}

		.checkbox {
			position: absolute;
			left: 10upx;
			z-index: 8;
			font-size: 44upx;
			line-height: 1;
			padding: 4upx;
			color: $font-color-disabled;
			background: #fff;
			border-radius: 50px;
		}

		.item-right {
			display: flex;
			flex-direction: column;
			flex: 1;
			overflow: hidden;
			position: relative;
			padding-left: 30upx;

			.price {
				font-size: $font-base + 7upx;
				color: #ff7144;
				height: 40upx;
				line-height: 40upx;
			}

			.attr {
				font-size: $font-sm + 2upx;
				color: $font-color-light;
				height: 50upx;
				line-height: 80upx;
			}

			.price {
				height: 50upx;
				line-height: 100upx;
			}
		}

		.del-btn {
			padding: 4upx 10upx;
			font-size: 34upx;
			height: 50upx;
			color: $font-color-light;
		}
	}

	/* 底部栏 */
	.action-section {
		/* #ifdef H5 */
		margin-bottom: 100upx;
		/* #endif */
		position: fixed;
		left: 30upx;
		bottom: 30upx;
		z-index: 95;
		display: flex;
		align-items: center;
		width: 690upx;
		height: 100upx;
		padding: 0 30upx;
		background: rgba(255, 255, 255, .9);
		box-shadow: 0 0 20upx 0 rgba(0, 0, 0, .5);
		border-radius: 16upx;

		.checkbox {
			height: 52upx;
			position: relative;

			image {
				width: 52upx;
				height: 100%;
				position: relative;
				z-index: 5;
			}
		}

		.clear-btn {
			position: absolute;
			left: 26upx;
			top: 0;
			z-index: 4;
			width: 0;
			height: 52upx;
			line-height: 52upx;
			padding-left: 38upx;
			font-size: $font-base;
			color: #fff;
			background: $font-color-disabled;
			border-radius: 0 50px 50px 0;
			opacity: 0;
			transition: .2s;

			&.show {
				opacity: 1;
				width: 120upx;
			}
		}

		.total-box {
			flex: 1;
			display: flex;
			flex-direction: column;
			text-align: right;
			padding-right: 40upx;

			.price {
				font-size: $font-lg;
				color: $font-color-dark;
			}

			.coupon {
				font-size: $font-sm;
				color: $font-color-light;

				text {
					color: $font-color-dark;
				}
			}
		}

		.confirm-btn {
			padding: 0 38upx;
			margin: 0;
			border-radius: 100px;
			height: 76upx;
			line-height: 76upx;
			font-size: $font-base + 2upx;
			background: $uni-color-primary;
			box-shadow: 1px 2px 5px rgba(217, 60, 93, 0.72)
		}
	}

	/* 复选框选中状态 */
	.action-section .checkbox.checked,
	.cart-item .checkbox.checked {
		color: $uni-color-primary;
	}

	.checkbox.checked {
		color: $uni-color-primary;
	}

	.caty-opster {
		display: inline-block;
		float: right;
	}

	.lochent {
		width: 90px !important;
		height: 90px !important;
	}

	.actype {
		float: right;
		height: 125px;
		position: absolute;
		background-color: #ee0a24;
		color: #fff;
		bottom: 0;
		font-size: 32upx;
		right: -65px;
		width: 65px;
	}

	.unlike {
		position: absolute;
		bottom: 30%;
		right: 20%;
	}

	.unlik {
		position: absolute;
		bottom: 50%;
		right: 32%;
	}

	.checkaas {
		margin-left: 10px;
		color: $uni-color-primary ;
	}

	.catyopn {
		margin-left: 15rpx;
	}
</style>